import { defineStore } from 'pinia'

export default defineStore('todos', {
  state: () => ({
    list: [
      {
        id: 1,
        name: '吃饭',
        done: true,
      },
      {
        id: 2,
        name: '睡觉',
        done: true,
      },
      {
        id: 3,
        name: '打豆豆',
        done: true,
      },
    ],
  }),
  actions: {
    changeDone(id) {
      const item = this.list.find((item) => item.id === id)
      item.done = !item.done
    },
    delTodo(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },
    addTodo(name) {
      this.list.unshift({
        id: Date.now(),
        name,
        done: false,
      })
    },
    // 全选/全不选
    checkAll(done) {
      this.list.forEach((item) => (item.done = done))
    },
    // 清空已完成/保留未完成
    clearCompleted() {
      this.list = this.list.filter((item) => !item.done)
    },
  },
  getters: {
    isCheckAll(state) {
      return state.list.every((item) => item.done)
    },
    // 剩余数量
    leftCount() {
      return this.list.filter((item) => !item.done).length
    },
  },
})
